#define  _CRT_SECURE_NO_WARNINGS
//1:
class Solution {
public:
    int Sum(int n)
    {
        int sum = 0;
        while (n > 0)
        {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        if (n == 1)return true;
        int slow = Sum(n);
        int fast = Sum(slow);
        while (fast != slow)
        {
            fast = Sum(fast);
            fast = Sum(fast);
            slow = Sum(slow);
        }
        return fast == 1;
    }
};
//2:
//class Solution {
//public:
//    int Sum(int n)
//    {
//        int sum = 0;
//        while (n > 0)
//        {
//            sum += (n % 10) * (n % 10);
//            n /= 10;
//        }
//        return sum;
//    }
//    bool isHappy(int n) {
//        time_t start = clock();
//        while (n != 1)
//        {
//            time_t cur = clock();
//            if (cur - start > 1000)return false;
//            n = Sum(n);
//        }
//        return true;
//    }
//};